class_name Stage
extends Node2D


@onready var checkpoints: Node2D = $Checkpoints
@onready var doors: Node2D = $Doors
@onready var playerSpawnLocation: Node2D = $PlayerSpawnLocation

## 场景音乐
@export var music: MusicManager.Music


func _init():
	StageManager.checkpoint_complete.connect(on_checkpoint_complete)


func _ready():
	MusicPlayer.play(music)

	set_checkpoints()

	await get_tree().process_frame
	door_orphan_actor()


func set_checkpoints():
	var checkpoint := checkpoints.get_child(-1)  as Checkpoint
	checkpoint.is_latest = true


func door_orphan_actor():
	for door: Node2D in doors.get_children():
		EntityManager.orphan_actor.emit(door)


func get_player_spawn_location() -> Vector2:
	return playerSpawnLocation.position


func on_checkpoint_complete(checkpoint: Checkpoint):
	if checkpoint.is_latest:
		StageManager.stage_complete.emit()
